import { apiService } from '@/api/apiService.js';
import { getSystemMessage, dealSystemMessage, dealAllMessages } from '@/api/server.js';
import { reactive } from 'vue';
import { useStore } from 'vuex';
import { selfI18n } from '../../i18n/index';
import { showSuccess } from '../../util/MessageDeal';
import moment from 'moment';
export function SystemMessageSetup () {
    const list_state = reactive({
        page_size: 17,
        page: 1,
        current_page: 1,
        total_num: 0,
        message_status: '1',
        search_content: '',
        message_data: [],
        searchmodel: {
            status_text: 1
        },
        message_data_table_props: [
            {
                id: 'time_text',
                label: selfI18n('message_date'),
                width: '150'
            }, {
                id: 'status_text',
                label: selfI18n('message_status'),
                width: '100'
            }
        ]
    });
    const store = useStore();

    const formatMessage = (messages) => {
        return messages.map((child) => {
            return {
                ...child,
                status_text: child.status === '2' ? selfI18n('message_had_deal') : selfI18n('message_un_deal'),
                time_text: moment(child.savetime).format('YYYY-MM-DD HH:mm:ss')
            };
        });
    };
    const search = (searchmodel) => {
        list_state.searchmodel = searchmodel || list_state.searchmodel;
        getMessage();
    };
    const refresh = () => {
        getMessage();
    };
    const getMessage = () => {
        const { msgcontent, status_text, time_text } = list_state.searchmodel;
        apiService({
            api: getSystemMessage,
            param: {
                n: list_state.page_size,
                p: list_state.page,
                params: msgcontent || '',
                status: status_text || '',
                endtime: (time_text && time_text[1]) || '',
                starttime: (time_text && time_text[0]) || '',
                msgtype: ''
            },
            success: (data) => {
                list_state.message_data = formatMessage(data.value);
                list_state.total_num = data.number;
            }
        });
    };

    const dealMessage = (eventid, params, callback) => {
        apiService({
            api: dealSystemMessage,
            param: {
                eventid,
                params
            },
            success: () => {
                refresh();
                store.dispatch('getSystemMessagePreview');
                showSuccess(selfI18n('message_deal_success'));
                callback && callback();
            }
        });
    };

    const setAllMessageRead = () => {
        apiService({
            api: dealAllMessages,
            param: {},
            success: () => {
                showSuccess(selfI18n('message_had_set_all_read'));
                store.dispatch('getSystemMessagePreview');
                refresh();
            }
        });
    };
    const filterAction = (row, action) => {
        if (!row) {
            return false;
        }
        const msgtype = row.msgtype;
        if (row.status === '2') {
            return false;
        }
        switch (msgtype) {
        case '1':
            if (action === 'deal') {
                return true;
            }
            return false;
        case '2':
            if (action === 'confirm') {
                return true;
            }
            return false;
        default:
            return false;
        }
    };
    const pageChange = (page) => {
        list_state.page = page;
        getMessage();
    };
    const sizeChange = (size) => {
        list_state.page = 1;
        list_state.page_size = size;
        getMessage();
    };

    return {
        list_state,
        setAllMessageRead,
        filterAction,
        sizeChange,
        search,
        pageChange,
        getMessage,
        dealMessage,
        formatMessage
    };
}
